package com.etang.a2020b1019;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020b1019.fragment.BarActivity;
import com.etang.a2020b1019.tools.AppConfig;
import com.etang.a2020b1019.tools.CodePass;
import com.etang.a2020b1019.tools.DiyToast;
import com.etang.a2020b1019.tools.MyDataBaseHelper;

import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_show_code;
    private TextView tv_time, tv_time_sx;
    private EditText et_user, et_pass, et_code_pass, et_ip;
    private SharedPreferences sharedPreferences;
    private SQLiteDatabase db;
    private MyDataBaseHelper dbHelper;
    private Button btn_login;
    private CheckBox cb_rember, cb_autologin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("登录");
        initView();
        initOpen();
        handler.post(timeRunna);
        iv_show_code.setImageBitmap(CodePass.createBitmap());
        iv_show_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_show_code.setImageBitmap(CodePass.createBitmap());
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_user.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "用户名不能为空");
                } else if (et_pass.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "密码不能为空");
                } else if (et_ip.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "IP地址不能为空");
                } else if (et_code_pass.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "验证码不能为空");
                } else {
                    if (et_code_pass.getText().toString().equalsIgnoreCase(CodePass.code)) {
                        Cursor cursor = db.rawQuery("select * from user where username = ? and passward = ?", new String[]{et_user.getText().toString(), et_pass.getText().toString()});
                        if (cursor.moveToNext()) {
                            if (cb_autologin.isChecked()) {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", true)
                                        .putBoolean("rember", true)
                                        .putString("user", et_user.getText().toString())
                                        .putString("pass", et_pass.getText().toString())
                                        .putString("ip", et_ip.getText().toString()).commit();
                            } else if (cb_rember.isChecked()) {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", false)
                                        .putBoolean("rember", true)
                                        .putString("user", et_user.getText().toString())
                                        .putString("pass", et_pass.getText().toString())
                                        .putString("ip", et_ip.getText().toString()).commit();
                            } else {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", false)
                                        .putBoolean("rember", false)
                                        .putString("user", et_user.getText().toString())
                                        .putString("pass", et_pass.getText().toString())
                                        .putString("ip", et_ip.getText().toString()).commit();
                            }
                            SocketClient.ip = et_ip.getText().toString();
                            startActivity(new Intent(getApplicationContext(), BarActivity.class));
                            finish();
                        } else {
                            DiyToast.showToast(getApplicationContext(), "用户名或密码输入错误");
                        }
                    } else {
                        DiyToast.showToast(getApplicationContext(), "验证码输入错误");
                    }
                }
            }
        });
    }

    private void initOpen() {
        if (sharedPreferences.getBoolean("is", false) == true) {
            if (sharedPreferences.getBoolean("rember", false) == true) {
                cb_rember.setChecked(true);
                et_pass.setText(sharedPreferences.getString("pass", null));
                et_ip.setText(sharedPreferences.getString("ip", null));
                et_user.setText(sharedPreferences.getString("user", null));
            }
            if (sharedPreferences.getBoolean("autologin", false) == true) {
                cb_autologin.setChecked(true);
                SocketClient.ip = sharedPreferences.getString("ip", null);
                startActivity(new Intent(getApplicationContext(), BarActivity.class));
                finish();
            }
        } else {
            sharedPreferences.edit().putBoolean("is", true).commit();
            sharedPreferences.edit().putString("smo_state", "开").commit();
            sharedPreferences.edit().putString("pm_state", "开").commit();
            sharedPreferences.edit().putString("temp_state", "开").commit();
            sharedPreferences.edit().putString("ill_state", "开").commit();
            sharedPreferences.edit().putString("per_state", "开").commit();

            sharedPreferences.edit().putString("temp_values_max", "0").commit();
            sharedPreferences.edit().putString("temp_values_low", "0").commit();
            sharedPreferences.edit().putString("ill_values_max", "0").commit();
            sharedPreferences.edit().putString("ill_values_low", "0").commit();
            sharedPreferences.edit().putString("pm_values_max", "0").commit();
            sharedPreferences.edit().putString("pm_values_low", "0").commit();
            sharedPreferences.edit().putString("smo_values", "0").commit();
            sharedPreferences.edit().putString("per_values", "0").commit();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            SimpleDateFormat simpleDateFormat_hour = new SimpleDateFormat("HH");
            SimpleDateFormat simpleDateFormat_date = new SimpleDateFormat("yyyy年MM月dd日");
            tv_time.setText(simpleDateFormat.format(new Date()));
            int time = Integer.valueOf(simpleDateFormat_hour.format(new Date()));
            if (time < 12) {
                tv_time_sx.setText("上午/" + simpleDateFormat_date.format(new Date()));
            } else {
                tv_time_sx.setText("下午/" + simpleDateFormat_date.format(new Date()));
            }
            handler.postDelayed(timeRunna, 1000);
        }
    };
    Runnable timeRunna = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    private void initView() {
        dbHelper = new MyDataBaseHelper(getApplicationContext(), "info.db", null, 2);
        db = dbHelper.getWritableDatabase();
        sharedPreferences = getSharedPreferences("rember", MODE_MULTI_PROCESS);
        iv_show_code = (ImageView) findViewById(R.id.iv_show_code);
        tv_time = (TextView) findViewById(R.id.tv_login_time);
        tv_time_sx = (TextView) findViewById(R.id.tv_time_sx);
        btn_login = (Button) findViewById(R.id.btn_login);
        et_code_pass = (EditText) findViewById(R.id.et_code_pass);
        et_ip = (EditText) findViewById(R.id.et_ip);
        et_pass = (EditText) findViewById(R.id.et_pass);
        et_user = (EditText) findViewById(R.id.et_user);
        cb_autologin = (CheckBox) findViewById(R.id.cb_autologin);
        cb_rember = (CheckBox) findViewById(R.id.cb_rember);
    }
}